using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using Snake;
using Snake.Logger;
using UnityEngine;


[DataContract]
[Serializable]
public class Item
{
    [DataMember] public int id;
    [DataMember] public int count;
}

[DataContract]
[Serializable]
public class Archive
{
    [DataMember] public string tag;
    [DataMember] public string name;
    [DataMember] private int num = 31;

    [DataMember] public int age;
    [DataMember] public List<Item> list = new List<Item>();
    [DataMember] public Dictionary<int, Item> dict = new Dictionary<int, Item>();
}

public class TestBF : MonoBehaviour
{
    void Start()
    {
        Log.Logger = new UnityLogger("Test");
        Save3();
        // Save();
        // Save2();
        // Load2();
    }

    void Save3()
    {
        var a = new Archive
        {
            name = "kx",
            age = 11,
            list = new List<Item>
            {
                new Item
                {
                    id = 1001,
                    count = 32,
                }
            },
            dict = new Dictionary<int, Item>
            {
                {
                    312, new Item
                    {
                        id = 213,
                        count = 331,
                    }
                }
            }
        };

        var ms = new MemoryStream();
        XmlUtility.ToXmlBinary(a, ms);
        ms.Seek(0, SeekOrigin.Begin);
        var b = XmlUtility.FromXmlBinary<Archive>(ms);
    }

    void Save2()
    {
        var a = new Archive
        {
            name = "kx",
            age = 11,
            list = new List<Item>
            {
                new Item
                {
                    id = 1001,
                    count = 32,
                }
            },
            dict = new Dictionary<int, Item>
            {
                {
                    312, new Item
                    {
                        id = 213,
                        count = 331,
                    }
                }
            }
        };
        var s = new DataContractSerializer(typeof(Archive));

        using (var fs = new FileStream("archive2.bin", FileMode.OpenOrCreate))
        {
            fs.SetLength(0);
            s.WriteObject(fs, a);
            fs.Flush();

            Log.Info(fs.Length);
        }

        using (var fs = new FileStream("archive3.bin", FileMode.OpenOrCreate))
        {
            var s2 = XmlDictionaryWriter.CreateBinaryWriter(fs);
            fs.SetLength(0);
            s.WriteObject(s2, a);
            s2.Flush();

            Log.Info(fs.Length);
        }
    }

    void Load2()
    {
        var s = new DataContractSerializer(typeof(Archive));

        using (var fs = new FileStream("archive2.bin", FileMode.OpenOrCreate))
        {
            var b = s.ReadObject(fs);
        }
    }

    void Load()
    {
        using (var fs = new FileStream("archive.bin", FileMode.OpenOrCreate))
        {
            var bf = new BinaryFormatter();
            var b = bf.Deserialize(fs);
        }
    }

    void Save()
    {
        var a = new Archive
        {
            name = "kx",
            age = 11,
            list = new List<Item>
            {
                new Item
                {
                    id = 1001,
                    count = 32,
                }
            },
            dict = new Dictionary<int, Item>
            {
                {
                    312, new Item
                    {
                        id = 213,
                        count = 331,
                    }
                }
            }
        };

        using var fs = new FileStream("archive.bin", FileMode.OpenOrCreate);
        var bf = new BinaryFormatter();
        bf.Serialize(fs, a);
        fs.Flush();
        Log.Info(fs.Length);
    }
}